package com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.bo.ITXingzhengfawenNeirongBO;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.FaWenInfo;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.TXingzhengfawenNeirong;
import com.tjsinfo.tjsoa.system.bo.IDepartmentBo;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.bo.ISystemUserDepartmentBo;
import com.tjsinfo.tjsoa.system.vo.TSystemDepartment;
import com.tjsinfo.tjsoa.system.vo.TSystemUserDepartment;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;
import com.tjsinfo.tjsoa.workflow.bo.ITaskInstanceBO;

/**
 * 查阅文件
 * @author Administrator
 *
 */
public class ChaYueWenJianAction extends Action{
	private ITXingzhengfawenNeirongBO xingzhengfawenNeirongBO;
	

	public ITXingzhengfawenNeirongBO getXingzhengfawenNeirongBO() {
		return xingzhengfawenNeirongBO;
	}


	public void setXingzhengfawenNeirongBO(
			ITXingzhengfawenNeirongBO xingzhengfawenNeirongBO) {
		this.xingzhengfawenNeirongBO = xingzhengfawenNeirongBO;
	}


	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		
//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.ss");   //调试用
				
		Integer first =  0;
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		String biaoti = request.getParameter("biaoti");
		String wenyear = request.getParameter("wenyear");
		String wentype = request.getParameter("wentype");
		String zhuangtai = request.getParameter("zhuangtai");
		String wenhao = request.getParameter("wenhaotype");
		String maintypeid = request.getParameter("maintypeid");
		String subtypename = request.getParameter("subtypename");
		List<FaWenInfo> faWenInfoList = new ArrayList<FaWenInfo>();
		
		if ("全部".equals(subtypename)){
			subtypename=null;
		}
		
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		
//		Calendar ctemp = Calendar.getInstance();    //调试用
//        System.out.println(df.format(ctemp.getTime())+"开始查询前20个文件");     //调试用
        
        
		if (request.getParameter("start") == null) {
			faWenInfoList = xingzhengfawenNeirongBO.selectChaYueWenJian(SessionUser.getUserId(request), 0, (0 + 20), biaoti, wenyear, subtypename, zhuangtai,wenhao,maintypeid);

		} else {
			 first = Integer.parseInt(request.getParameter("start"));
			faWenInfoList =  xingzhengfawenNeirongBO.selectChaYueWenJian(SessionUser.getUserId(request), (first + 1), (first + 20) , biaoti, wenyear, subtypename, zhuangtai,wenhao,maintypeid);

			
		}
		
//	    ctemp = Calendar.getInstance();    //调试用
//        System.out.println(df.format(ctemp.getTime())+"开始查询文件数");     //调试用
        
		Integer totalProperty = xingzhengfawenNeirongBO.selectChaYueWenJianCount(SessionUser.getUserId(request) , biaoti, wenyear, subtypename, zhuangtai,wenhao,maintypeid);		
		object.put("totalProperty", totalProperty);
		
//		ctemp = Calendar.getInstance();    //调试用
//	    System.out.println(df.format(ctemp.getTime())+"开始赋值");     //调试用
		
	   for (FaWenInfo faWenInfo : faWenInfoList) {
			if(faWenInfo.getId() != null){
				JSONObject object2 = new JSONObject(faWenInfo);
				if (faWenInfo.getWentype().indexOf("纪要") > -1) {
					object2.put("wenhaotype", faWenInfo.getWenhaotype().substring(0, faWenInfo.getWenhaotype().length() - 1) + "次");
				} else {
					;
				}
				
				if ("xingzhengfawen".equals(faWenInfo.getTableName())) {
					object2.put("operate", "<a href='"+basePath+"tjsinfo/tjsoa/xingzhengfawen/shenpi.do?method=selectonedoc&id="+faWenInfo.getId()+"'  target='_blank'>查看</a>"+
						"&nbsp;&nbsp;<a href='"+basePath+"documentationflow/xingzhengfawen/fubenlist.jsp?id="+faWenInfo.getId()+"'>查看副本</a>");
				}else if ("dangweifawen".equals(faWenInfo.getTableName())) {
					object2.put("operate", "<a href='"+basePath+"tjsinfo/tjsoa/dangweifawen/shenpi.do?method=selectonedoc&id="+faWenInfo.getId()+"' target='_blank'>查看</a>"+
							"&nbsp;&nbsp;<a href='"+basePath+"documentationflow/dangweifawen/fubenlist.jsp?id="+faWenInfo.getId()+"'>查看副本</a>");
				}else if ("tongzhifawen".equals(faWenInfo.getTableName())) {
					object2.put("operate", "<a href='"+basePath+"tjsinfo/tjsoa/tongzhifawen/shenpi.do?method=selectonedoc&id="+faWenInfo.getId()+"' target='_blank'>查看</a>");
				}else if ("xingzhengfawenfengfa".equals(faWenInfo.getTableName())) {
					object2.put("operate", "<a href='"+basePath+"tjsinfo/tjsoa/xingzhengfawen/shenpi.do?method=fengfaselectonedoc&id="+faWenInfo.getId()+"'  target='_blank'>查看</a>"+
							"&nbsp;&nbsp;<a href='"+basePath+"documentationflow/xingzhengfawen/fubenlist.jsp?id="+faWenInfo.getId()+"'>查看副本</a>");
				}else if ("dangweifawenfengfa".equals(faWenInfo.getTableName())) {
						object2.put("operate", "<a href='"+basePath+"tjsinfo/tjsoa/dangweifawen/shenpi.do?method=fengfaselectonedoc&id="+faWenInfo.getId()+"' target='_blank'>查看</a>"+
								"&nbsp;&nbsp;<a href='"+basePath+"documentationflow/dangweifawen/fubenlist.jsp?id="+faWenInfo.getId()+"'>查看副本</a>");
				}

				array.put(object2);
			}
			
		}
		object.put("root", array);
		
//		 ctemp = Calendar.getInstance();    //调试用
//	     System.out.println(df.format(ctemp.getTime())+"返回结果");     //调试用
		response.getWriter().print(object);
		return null;
	}
	
}

